package com.hksj.cm.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.cbm.service.DeviceInstanceService;
import com.hksj.cbm.util.EasyExcelUtil;
import com.hksj.cbm.vo.SpaceModelVO;
import com.hksj.cm.dto.DeviceAlarmDTO;
import com.hksj.cm.mapper.DeviceAlarmMapper;
import com.hksj.cm.vo.DeviceAlarmRankVO;
import com.hksj.cm.vo.DeviceAlarmVO;
import com.hksj.cm.vo.ExportDeviceAlarmVO;
import com.hksj.common.core.common.Constants;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DeviceAlarmService
 * @Description TODO
 * @Author @陈章
 * @Date 2024/7/30 10:18
 * @Version 1.0
 */
@Service
public class DeviceAlarmService {
    @Autowired
    private DeviceAlarmMapper mapper;
    @Autowired
    private DeviceInstanceService deviceInstanceService;

    public Page<DeviceAlarmVO> deviceAlarmList(DeviceAlarmDTO dto) {
        dto.setCode(SpecialityModelCodeEnum.cleanliness.getCode());
        Page<DeviceAlarmVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<DeviceAlarmVO> list = mapper.deviceAlarmList(page, dto);
        List<String> pathList = list.stream().map(DeviceAlarmVO::getPaths).filter(Objects::nonNull).collect(Collectors.toList());
        for (DeviceAlarmVO deviceAlarmVO : list) {
            if (!CollectionUtils.isEmpty(pathList)) {
                Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
                if (CollUtil.isNotEmpty(pathNameMap)) {
                    List<SpaceModelVO> spaceModelVOList = pathNameMap.get(deviceAlarmVO.getPaths());
                    if (CollUtil.isNotEmpty(spaceModelVOList)) {
                        Map<String, String> spaceNameMap = spaceModelVOList.stream()
                                .collect(Collectors.toMap(p -> p.getSpaceItemValue(), p -> p.getModelName()));
                        // 楼栋-楼层
                        StringBuilder builder = new StringBuilder(spaceNameMap.get(Constants.FOUR_STR));
                        if (StringUtils.isNotBlank(spaceNameMap.get(Constants.THREE_STR))) {
                            builder.append("-").append(spaceNameMap.get(Constants.THREE_STR));
                        }
                        deviceAlarmVO.setDeviceLocation(builder.toString());
                    }
                    deviceAlarmVO.setSpaceModelVOList(spaceModelVOList);
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    public int deviceAlarmCount(DeviceAlarmDTO dto) {
        dto.setCode(SpecialityModelCodeEnum.cleanliness.getCode());
        return mapper.deviceAlarmCount(dto);
    }

    public List<DeviceAlarmRankVO> deviceAlarmRank(DeviceAlarmDTO dto) {
        dto.setCode(SpecialityModelCodeEnum.cleanliness.getCode());
        List<DeviceAlarmRankVO> list = mapper.deviceAlarmRank(dto);
        List<String> pathList = list.stream().map(DeviceAlarmRankVO::getPaths).filter(Objects::nonNull).collect(Collectors.toList());
        for (DeviceAlarmRankVO deviceAlarmRankVO : list) {
            if (!CollectionUtils.isEmpty(pathList)) {
                Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
                if (CollUtil.isNotEmpty(pathNameMap)) {
                    List<SpaceModelVO> spaceModelVOList = pathNameMap.get(deviceAlarmRankVO.getPaths());
                    if (CollUtil.isNotEmpty(spaceModelVOList)) {
                        Map<String, String> spaceNameMap = spaceModelVOList.stream()
                                .collect(Collectors.toMap(p -> p.getSpaceItemValue(), p -> p.getModelName()));
                        // 楼栋-楼层
                        StringBuilder builder = new StringBuilder(spaceNameMap.get(Constants.FOUR_STR));
                        if (StringUtils.isNotBlank(spaceNameMap.get(Constants.THREE_STR))) {
                            builder.append("-").append(spaceNameMap.get(Constants.THREE_STR));
                        }
                        deviceAlarmRankVO.setDeviceLocation(builder.toString());
                    }
                    deviceAlarmRankVO.setSpaceModelVOList(spaceModelVOList);
                }
            }
        }
        return list;
    }

    public void exportAlarm(DeviceAlarmDTO dto, HttpServletResponse response) {
        dto.setCode(SpecialityModelCodeEnum.cleanliness.getCode());
        List<DeviceAlarmVO> list = mapper.deviceAlarmList(new Page<>(), dto);
        List<String> pathList = list.stream().map(DeviceAlarmVO::getPaths).filter(Objects::nonNull).collect(Collectors.toList());
        List<ExportDeviceAlarmVO> exportList = new ArrayList<>();
        for (DeviceAlarmVO deviceAlarmVO : list) {
            int i = 1;
            ExportDeviceAlarmVO exportVO = new ExportDeviceAlarmVO();
            if (!CollectionUtils.isEmpty(pathList)) {
                Map<String, List<SpaceModelVO>> pathNameMap = deviceInstanceService.devicePathConvertToUTF8(pathList);
                if (CollUtil.isNotEmpty(pathNameMap)) {
                    List<SpaceModelVO> spaceModelVOList = pathNameMap.get(deviceAlarmVO.getPaths());
                    if (CollUtil.isNotEmpty(spaceModelVOList)) {
                        Map<String, String> spaceNameMap = spaceModelVOList.stream()
                                .collect(Collectors.toMap(p -> p.getSpaceItemValue(), p -> p.getModelName()));
                        // 楼栋-楼层
                        StringBuilder builder = new StringBuilder(spaceNameMap.get(Constants.FOUR_STR));
                        if (StringUtils.isNotBlank(spaceNameMap.get(Constants.THREE_STR))) {
                            builder.append("-").append(spaceNameMap.get(Constants.THREE_STR));
                        }
                        exportVO.setDeviceLocation(builder.toString());
                    }
                    deviceAlarmVO.setSpaceModelVOList(spaceModelVOList);
                }
            }
            exportVO.setCreateTime(deviceAlarmVO.getCreateTime());
            exportVO.setDeviceName(deviceAlarmVO.getDeviceName());
            exportVO.setProperty(deviceAlarmVO.getProperty());
            exportVO.setValue(deviceAlarmVO.getValue());
            exportVO.setSeq(String.valueOf(i++));
            exportList.add(exportVO);
        }
        String fileName = new StringBuffer()
                .append("洁净度监测报警记录")
                .append(new SimpleDateFormat("_yyyyMMddHHmmss").format(new Date()))
                .toString();

        EasyExcelUtil.exportExcelData(response, fileName, exportList, ExportDeviceAlarmVO.class);
    }
}
